package com.tong.hao.ac.service.controller.query;

import cn.buddha.jec.model.head.TongPageHead;
import cn.buddha.jec.util.TongBusinessUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tong.hao.ac.api.feign.FeignConf;
import com.tong.hao.ac.api.model.in.query.QueryUserInfoIn;
import com.tong.hao.ac.api.model.out.query.QueryUserInfoOut;
import com.tong.hao.ac.api.model.out.query.list.UserInfo;
import com.tong.hao.ac.api.service.query.IQueryUserInfoController;
import com.tong.hao.ac.db.dao.UsersMapper;
import com.tong.hao.ac.db.entity.Users;
import com.tong.hao.ac.service.transfer.query.QueryUserInfoTransfer;
import com.tong.hao.ac.service.transfer.query.QueryUserInfoOutTransfer;
import com.tong.hao.common.annotation.HaoValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ClassName QueryUserInfoController
 * @Author 友野浩二
 * @Description 客户信息查询
 * @Version 1.0
 */
@Api(tags = "客户信息查询")
@RestController
@Slf4j
public class QueryUserInfoController implements IQueryUserInfoController {

    @Resource
    UsersMapper usersMapper;

    @Autowired
    private QueryUserInfoTransfer queryUserInfoTransferImpl;

    @Autowired
    private QueryUserInfoOutTransfer queryUserInfoOutTransferImpl;

    @Autowired
    private FeignConf feignConf;

    @ApiOperation(value = "客户信息查询")
    @RequestMapping(value = QUERY_USER_INFO)
    @HaoValidate
    @Override
    public QueryUserInfoOut queryUserInfo(@RequestBody QueryUserInfoIn queryUserInfoIn) {
        log.info("com.tong.hao.ac.service.controller.query.QueryUserInfoController.queryUserInfo starting...");

        // ribbon重试机制测试
        try {
            int sleepTime = new Random().nextInt(3000);
            Thread.sleep(sleepTime);
            System.err.println("sleepTime: " + sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 使用mapstruct进行对象属性赋值
        Users usersCondition = queryUserInfoTransferImpl.to(queryUserInfoIn);
        log.info(" =================> {}", usersCondition);

        System.err.println("FeignConf.get(applicationName1): " + feignConf);
        System.err.println("FeignConf.get(applicationName2): " + FeignConf.get("applicationName"));

        List<Users> usersList = this.usersMapper.selectList(null);

        List<UserInfo> userInfoList = new ArrayList<>();
        usersList.forEach((users) -> {
            UserInfo userInfo = new UserInfo();
            userInfo.setAge(users.getAge());
            userInfo.setFullName(users.getFullName());
            userInfo.setEmail(users.getEmail());
            userInfoList.add(userInfo);
        });
        QueryUserInfoOut queryUserInfoOut = new QueryUserInfoOut();
        queryUserInfoOut.setUserInfoList(userInfoList);

        return queryUserInfoOut;
    }

    /**
     * @return com.tong.hao.ac.api.model.out.query.QueryUserInfoOut
     * @Author 友野浩二
     * @Description // 分页查询
     * @Param [queryUserInfoIn]
     */
    @ApiOperation(value = "客户信息分页查询")
    @RequestMapping(value = QUERY_USER_INFO_PAGE, method = RequestMethod.POST)
    @HaoValidate
    @Override
    public QueryUserInfoOut queryPageUserInfo(@RequestBody QueryUserInfoIn queryUserInfoIn) {
        // 获取分页信息
        TongPageHead tongInPageHead = queryUserInfoIn.getTongPageHead();

        // 分页查询
        // 1- 分页查询条件
        Page<Users> page = new Page<>(tongInPageHead.getCurrentPage(), tongInPageHead.getSize());

        // 查询条件构造器
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(Users::getFullName, queryUserInfoIn.getFullName());

        // 2- 分页查询执行
        IPage<Users> usersPage = usersMapper.selectPage(page, queryWrapper);
        // 3- 分页结果处理
        // 获取查询结果集
        List<Users> usersResultList = usersPage.getRecords();
        List<UserInfo> userInfos = queryUserInfoOutTransferImpl.to(usersResultList);

        QueryUserInfoOut queryUserInfoOut = new QueryUserInfoOut();
        queryUserInfoOut.setUserInfoList(userInfos);

        try {
//            int a = 1 / 0;
        } catch (Exception e) {
            log.error("异常啦!", e);
            // 用来测试
//            throw TongBusinessUtils.createBusinessException("000", "567890");
        }

        // TODO 先暂时这样写，后续再处理
        TongPageHead tongPageHead = new TongPageHead();
        tongPageHead.setTotal(usersPage.getTotal());
        tongPageHead.setPages(usersPage.getPages());
        tongPageHead.setCurrentPage(usersPage.getCurrent());
        tongPageHead.setSize(usersPage.getSize());
        queryUserInfoOut.setTongPageHead(tongPageHead);

        return queryUserInfoOut;
    }

    @ApiOperation(value = "Mapper.xml查询")
    @RequestMapping(value = QUERY_USER, method = RequestMethod.POST)
    @HaoValidate
    @Override
    public QueryUserInfoOut queryUser(@RequestBody QueryUserInfoIn queryUserInfoIn) {
        List<Users> usersResultList = this.usersMapper.selectByFullName(queryUserInfoIn.getFullName());
        List<UserInfo> userInfos = queryUserInfoOutTransferImpl.to(usersResultList);

        QueryUserInfoOut queryUserInfoOut = new QueryUserInfoOut();
        queryUserInfoOut.setUserInfoList(userInfos);
        return queryUserInfoOut;
    }
}
